package com.zhidisoft.blog.admin.controller;

import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.zhidisoft.blog.admin.entity.Article;
import com.zhidisoft.blog.admin.entity.ArticleType;
import com.zhidisoft.blog.admin.service.IArticleService;
import com.zhidisoft.blog.admin.service.IArticleTypeService;
import com.zhidisoft.blog.admin.vo.AjaxVo;
import com.zhidisoft.blog.admin.vo.ArticleVo;

@Controller
@RequestMapping("/article")
public class ArticleController extends BaseController {

	@Autowired
	IArticleService articleService;	
	@Autowired
	IArticleTypeService articleTypeService;
	@GetMapping("/save")
	public ModelAndView saveUI(){
		List<ArticleType> articleTypeList = articleTypeService.findAll();
		ModelAndView mav = new ModelAndView("article/form.html");
		mav.addObject("typeList", articleTypeList);
		return mav;
	}
	/**
	 * 帮助页
	 * @return
	 */
	@GetMapping("/adminhelp")
	public String admin(){
		return "article/admin-help.html";
	}
	/**
	 * 相册
	 * @return
	 */
	@GetMapping("/admingallery")
	public String admina(){
		return "article/admin-gallery.html";
	}
	/**
	 * 个人资料
	 * @return
	 */
	@GetMapping("/adminuser")
	public String adminu(){
		return "article/admin-user.html";
	}
	/**
	 * 首页
	 * @return
	 */
	@GetMapping("/adminindex")
	public String admini(){
		return "article/admin-index.html";
	}
	/**
	 * 系统日志
	 * @return
	 */
	@GetMapping("/adminlog")
	public String adminl(){
		return "article/admin-log.html";
	}
	/**
	 * 404
	 * @return
	 */
	@GetMapping("/admin404")
	public String admin4(){
		return "article/admin-404.html";
	}
	/**
	 * 表格
	 * @return
	 */
	@GetMapping("/admintable")
	public String adminT(){
		return "article/admin-table.html";
	}
	/**
	 * 表单
	 * @return
	 */
	@GetMapping("/adminform")
	public String adminI(){
		return "article/admin-form.html";
	}
	
	/*@PostMapping("/save")
	public String save(ArticleVo articleVo, HttpSession session,
			BindingResult result){
		Article article = new Article();
		article.setId(UUID.randomUUID().toString().replace("-", ""));
		article.setUserId(session.getAttribute("userId").toString());
		article.setInfo(articleVo.getInfo());
		article.setInfoTitle(articleVo.getInfoTitle());
		article.setTypeId(articleVo.getTypeId());
		article.setTitle(articleVo.getTitle());
		articleService.save(article);
		return "redirect:/article/list";
	}*/

	/*@PostMapping("/save")
	public ModelAndView save(@Valid ArticleVo articleVo,BindingResult result,HttpSession session,
			RedirectAttributes redirectAttributes){
		//RedirectAttributes重定向属性，用于重定向传递参数
		//重定向list页面
		ModelAndView mav = new ModelAndView("redirect:/article/list");
		if (result.hasErrors()) {
			// 2.重定向传递参数
			mav = new ModelAndView("redirect:/common/err");
			redirectAttributes.addFlashAttribute("errorList", result.getAllErrors());
			return mav;
		}
		Article article = new Article();
		article.setId(UUID.randomUUID().toString().replace("-", ""));
		article.setUserId(session.getAttribute("userId").toString());
		article.setInfo(articleVo.getInfo());
		article.setInfoTitle(articleVo.getInfoTitle());
		article.setTypeId(articleVo.getTypeId());
		article.setTitle(articleVo.getTitle());
		articleService.save(article);
		return mav;
}*/
	@PostMapping("/save")
	@ResponseBody
	public AjaxVo save(@Valid ArticleVo articleVo, BindingResult result) {
		if (result.hasErrors()) {
			for (ObjectError error : result.getAllErrors()) {
				return new AjaxVo().setErrorCode("10002").setErrorMsg(error.getDefaultMessage());
			}
		}
		Article article = new Article();
		article.setId(UUID.randomUUID().toString().replace("-", ""));
		article.setUserId(getCurUser());
		article.setInfo(articleVo.getInfo());
		article.setInfoTitle(articleVo.getInfoTitle());
		article.setTypeId(articleVo.getTypeId());
		article.setTitle(articleVo.getTitle());
		articleService.save(article);
		return new AjaxVo().setErrorCode("0");
	}
}